<?php

namespace App\Http\Controllers\Api\Syj;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HomeType extends Controller
{
    //房源分类
    public function homeType(){
//        创建数据集 创建关系
//        1.获取数据集（数组）$data = ['car'=>'车']//优先父类名
//        2.对表查询关键数据（json）->数组
//        3.循环(定义id,name)再循环（判断表'type'）[子->父]
        $data = [
            'house_type'=>'房子类型',
            'house'=>'房子'
        ];
        $getuserinforesourcelist = DB::select("SELECT id,name,type FROM house_resources where status = 0");
        $getuserinforesourcelist = json_decode(json_encode($getuserinforesourcelist),true);
        foreach ($data as $key => $v){
            $res['id'] = $key;
            $res['name'] = $v;
            $res['children'] = [];
            foreach ($getuserinforesourcelist as $k => $val){
                if($val['type'] == $key){
                    $info['ids'] = $val['id'];
                    $info['name'] = $val['name'];
                    $info['type'] = $val['type'];
                    $res['children'][] = $info;
                }
            }
            $list[] = $res;
        }
        return $list;
    }
}
